package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkOfficeType;
import com.ruoyi.work.service.IWorkOfficeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMDZ
 * @Desc: TODO 事务种类
 */
@RestController
@RequestMapping("/work/office")
public class WorkOfficeTypeController extends BaseController {

    @Autowired
    private IWorkOfficeTypeService workOfficeTypeService;

    /**
     * 获取事务种类列表
     */
    @PreAuthorize("@ss.hasPermi('work:office:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOfficeType workOfficeType) {
        startPage();
        List<WorkOfficeType> list = workOfficeTypeService.selectWorkOfficeTypeList(workOfficeType);
        return getDataTable(list);
    }

    /**
     * 详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:office:query')")
    @GetMapping(value = "/{officeId}")
    public AjaxResult getInfo(@PathVariable("officeId") Long officeId) {
        return AjaxResult.success(workOfficeTypeService.selectWorkOfficeTypeById(officeId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('work:office:add')")
    @Log(title = "新增事务种类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkOfficeType workOfficeType) {
        if (UserConstants.NOT_UNIQUE.equals(workOfficeTypeService.checkOfficeNameUnique(workOfficeType))) {
            return AjaxResult.error("新增事务种类信息'" + workOfficeType.getOfficeName() + "'失败，事务名称已存在");
        }
        workOfficeType.setCreateBy(getUsername());
        return toAjax(workOfficeTypeService.insertWorkOfficeType(workOfficeType));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('work:office:edit')")
    @Log(title = "修改事务种类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOfficeType workOfficeType) {
        if (UserConstants.NOT_UNIQUE.equals(workOfficeTypeService.checkOfficeNameUnique(workOfficeType))) {
            return AjaxResult.error("修改事务种类信息'" + workOfficeType.getUserName() + "'失败，事务名称已存在");
        }
        workOfficeType.setUpdateBy(getUsername());
        return toAjax(workOfficeTypeService.updateWorkOfficeType(workOfficeType));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('work:office:remove')")
    @Log(title = "删除事务种类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{officeIds}")
    public AjaxResult remove(@PathVariable Long[] officeIds) {
        return toAjax(workOfficeTypeService.deleteWorkOfficeTypeByIds(officeIds));
    }

    /**
     * 选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        List<WorkOfficeType> types = workOfficeTypeService.selectOfficeTypeAll();
        return AjaxResult.success(types);
    }
}
